﻿using System;
using BulletSharp;
using BulletSharp.Math;

namespace Painter3D.PhysicalSys
{
    /// <summary>
    /// 该类型自动同步位置信息,并保存一个对渲染系统的对象指针以便于访问
    /// </summary>
    internal class MotionStateUpdate : MotionState
    {
        public Transform transform
        {
            get; private set;
        }

        public MotionStateUpdate(Transform transform)
        {
            this.transform = transform ?? throw new ArgumentNullException(nameof(transform));
        }

        public override void GetWorldTransform(out Matrix worldTrans)
        {
            worldTrans = transform.Mat.Convert2BT();
        }

        public override void SetWorldTransform(ref Matrix worldTrans)
        {
            worldTrans.Decompose(out Vector3 scale, out Quaternion rotation, out Vector3 position);
            //赋值到世界坐标系下的位置和旋转【问题】
            transform.position = position.Convert2TK();
            transform.quaternion = rotation.Convert2TK();
        }
    }

}
